package com.lx.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lx.entity.LxUser;
import com.lx.mapper.LxUserMapper;
import com.lx.service.impl.LxUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 赵敬轩
 * @since 2021-09-12
 */
@RestController
@RequestMapping("/lx-user")
public class LxUserController {
    @Autowired
    private LxUserServiceImpl lxUserService;


    @CrossOrigin
    @GetMapping("/getAll")
    public String getAllUser(Integer page, Integer limit){
        IPage<LxUser> lxUserIPage = lxUserService.selectAllUser(page, limit);
        List<LxUser> list = lxUserService.list();
        System.out.println("list"+list);
        System.out.println("record"+lxUserIPage.getRecords());
        Map<String,Object> resultData = new HashMap<>();
        resultData.put("code",0);
        resultData.put("msg",null);
        resultData.put("data",lxUserIPage.getRecords());
        resultData.put("count",list.size());
       return JSON.toJSONString(resultData);
    }

    @CrossOrigin
    @GetMapping("/delete")
    public String deleteProvider(String id){
        HashMap<String , Object> resultData = new HashMap<>();
        System.out.println(id);

        boolean deleteUserId = lxUserService.removeById(id);
        if (deleteUserId)
            resultData.put("msg","删除成功");
        else
            resultData.put("msg","删除失败");
        return JSON.toJSONString(resultData);
    }


    @CrossOrigin
    @PostMapping("/save")
    public String saveUser(@RequestBody LxUser lxUser){
        System.out.println(lxUser);
        HashMap<String, Object> resultData = new HashMap<>();
        boolean save = lxUserService.saveOrUpdate(lxUser);
        if (save)
            resultData.put("msg","信息保存成功");
        else
            resultData.put("msg","信息保存失败");
        return JSON.toJSONString(resultData);

    }

    @CrossOrigin
    @GetMapping("/search")
    public String search(String uid, String uname, Integer page, Integer limit) {
        System.out.println(page + " " + limit + " " + uid + " " + uname);

        if (uid.isEmpty() && uname.isEmpty()) {
            IPage<LxUser> lxUserIPage = lxUserService.selectAllUser(page, limit);
            List<LxUser> list = lxUserService.list();
            Map<String, Object> resultData = new HashMap<>();
            resultData.put("code", 0);
            resultData.put("msg", null);
            resultData.put("data", lxUserIPage.getRecords());
            resultData.put("count", list.size());
            return JSON.toJSONString(resultData);
        } else {
            if (uid.isEmpty()) {
                IPage<LxUser> lxUserIPage = lxUserService.selectAllUser(page, limit);
                Map<String, Object> columnMap = new HashMap<String, Object>();
                List list = lxUserService.selectByName(uname);
                List<LxUser> userList = new ArrayList<>();
                Map<String, Object> resultData = new HashMap<>();
                resultData.put("code", 0);
                resultData.put("msg", null);
                resultData.put("data", list);
                resultData.put("count", list.size());
                return JSON.toJSONString(resultData);
            } else {
                LxUser user = lxUserService.getById(uid);
                List<LxUser> list = new ArrayList<>();
                list.add(user);
                System.out.println("controller的" + list);
                Map<String, Object> resultData = new HashMap<>();
                resultData.put("code", 0);
                resultData.put("msg", null);
                resultData.put("data", list);
                resultData.put("count", 1);
                return JSON.toJSONString(resultData);
            }

        }
    }
    @CrossOrigin
    @PostMapping("/updateUserName")
    public String updateUserName(Integer uid, String newname){
        System.out.println(uid + " " + newname);
        HashMap<String, Object> resultDate = new HashMap<>();
        LxUser user = lxUserService.getById(uid);
        if(user!=null){
            user.setUName(newname);
            boolean updatename = lxUserService.updateById(user);
            if (updatename){
                resultDate.put("msg","用户名更新成功");
            } else {
                resultDate.put("msg","用户名更新失败");
            }
        }
        return JSON.toJSONString(resultDate);
    }

    @CrossOrigin
    @PostMapping("/updateUserPwd")
    public String updateUserPwd(Integer uid, String newpassword){
        HashMap<String, Object> resultDate = new HashMap<>();
        LxUser user = lxUserService.getById(uid);
        if(user!=null){
            user.setUPassword(newpassword);
            boolean updatename = lxUserService.updateById(user);
            if (updatename){
                resultDate.put("msg","密码更新成功");
            } else {
                resultDate.put("msg","密码更新失败");
            }
        }
        return JSON.toJSONString(resultDate);
    }
}
